package yao.design.builder;

import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * builder模式创建
 * 如果类的构造器或者静态工厂中具有多个参数，设计这种类时，Builder模式就是种不错的选择，
 * 特别是当大多数参数都是可选的时候，与使用传统的重叠构造器模式相比，使用Builder模式的客户端代码更易于阅读和编写
 * 构建器也比JavaBean更加安全
 * @author chenyao
 *
 */
public class Person {
	//必须要的属性 required
	private final String name;
	private final String sex;
	
	//可选属性 optional 
	private final Integer age;
	private final String hobby;			//爱好
	private final String occupation;	//职业
	
	private Person(Builder builder) {
		this.name = builder.name;
		this.sex = builder.sex;
		this.age = builder.age;
		this.hobby = builder.hobby;
		this.occupation = builder.occupation;
	}
	public static class Builder {
		private final String name;
		private final String sex;
		
		private Integer age = 0;
		private String hobby = "";			//爱好
		private String occupation = "";	//职业
		public Builder(String name, String sex) {
			super();
			this.name = name;
			this.sex = sex;
		}
		
		public Builder age(Integer age) {
			this.age = age;
			return this;
		}

		public Builder hobby(String hobby) {
			this.hobby = hobby;
			return this;
		}

		public Builder occupation(String occupation) {
			this.occupation = occupation;
			return this;
		}

		public Person build() {
			return new Person(this);
		}
	}
	public String getName() {
		return name;
	}
	public String getSex() {
		return sex;
	}
	public Integer getAge() {
		return age;
	}
	public String getHobby() {
		return hobby;
	}
	public String getOccupation() {
		return occupation;
	}
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	
}
